// packageJ/supportCenter/supportCenterIndex/supportCenterIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    info: [],
    list: [],

    isLoadMore: true, //判断是否要加载更多的标识
    page: 1, //当前的页数
    total_page: 0, //总共有多少页
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  tapActive(e) {
    this.init();
    this.setData({
      active: e.currentTarget.dataset.key
    })
    this.getData();
  },
  init() {
    this.setData({
      list: [],
      page: 1,
      total_page: 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(this.data.isLoadMore)
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  checkTap(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    console.log(item, index)
    if (item.checked) {
      // this.list[index].checked = false;
      // this.$set(this.list, index, this.list[index]);
      this.setData({
        ['list[' + index + '].checked']: false
      })
      console.log(this.data.list[index])
    } else {
      this.getDaysLog(item.days, index);
    }
  },
  getDaysLog(day, index) {
    app._postNetWork({
      url: app.getNetAddresss('plugin.support-center.frontend.write-off-log.get-write-off-log'),
      data: {
        days: day
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ['list[' + index + '].checked']: true,
            ['list[' + index + '].log']: res.data.data,
            ['list[' + index + '].page']: res.data.current_page,
            ['list[' + index + '].last_page']: res.data.last_page,
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  moreLogTap(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    console.log(item, index)
    if (!item.checked && item.page >= item.last_page) {
      return;
    }
    let page = Number(item.page) + 1;
    app._postNetWork({
      url: app.getNetAddresss('plugin.support-center.frontend.write-off-log.get-write-off-log'),
      data: {
        days: item.days,
        page: page
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ['list[' + index + '].checked']: true,
            ['list[' + index + '].log']: this.data.list[index].log.concat(res.data.data),
            ['list[' + index + '].page']: res.data.current_page,
            ['list[' + index + '].last_page']: res.data.last_page,
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getMoreData() {
    let url = "";
    if (this.data.active == 0) {
      url = app.getNetAddresss("plugin.support-center.frontend.write-off-log.get-write-off-log-to-days");
    } else {
      url = app.getNetAddresss("plugin.support-center.frontend.write-off-log.get-quota-log");
    }
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      app._postNetWork({
        url: url,
        data: {
          page: this.data.page
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.data;
            this.setData({
              list: this.data.list.concat(myData)
            })
            this.data.isLoadMore = true;
          } else {
            app.tips(res.msg)
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  },
  getData() {
    let url = "";
    let json = {
      page: this.data.page
    };
    if (this.data.active == 0) {
      url = app.getNetAddresss("plugin.support-center.frontend.write-off-log.get-write-off-log-to-days");
    } else {
      url = app.getNetAddresss("plugin.support-center.frontend.write-off-log.get-quota-log");
    }
    app._postNetWork({
      url: url,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data.data,
            isLoadMore: true,
            total_page: res.data.last_page,
          })
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getUser() {
    let urlStr = app.getNetAddresss('plugin.support-center.frontend.write-off-log.get-member-info');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          // console.log(res)
          this.setData({
            info: res.data
          })
          wx.setNavigationBarTitle({
            title: res.data.plugin_name,
          })
          this.getData();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
})